package cn.tedu.yl.user.controller;

import cn.tedu.yl.base.response.JsonResult;
import cn.tedu.yl.base.response.PageData;
import cn.tedu.yl.user.pojo.dto.UserDelParam;
import cn.tedu.yl.user.pojo.dto.UserLoginParam;
import cn.tedu.yl.user.pojo.dto.UserQuery;
import cn.tedu.yl.user.pojo.dto.UserSaveParam;
import cn.tedu.yl.user.pojo.vo.UserVO;
import cn.tedu.yl.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public JsonResult login(@Validated @RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录:参数={}",userLoginParam);
        //调用业务层的login方法
        //Ctrl去接口  Ctrl+Alt去接口实现类  Ctrl+Shift去方法返回值类
        UserVO userVO = userService.login(userLoginParam);
        //将业务层返回的userVO对象封装到JsonResult中返回给前端
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public JsonResult saveUser(@RequestBody UserSaveParam userSaveParam){
        log.debug("保存用户:参数={}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "查询用户列表")
    @GetMapping("list")
    public JsonResult getList(@Validated UserQuery userQuery){
        PageData pageData = userService.getList(userQuery);
        return JsonResult.ok(pageData);
    }

    @ApiOperation(value = "查询用户详情")
    @GetMapping("detail/{id}")
    public JsonResult detail(@PathVariable("id") Long id){
        UserVO userVO = userService.detail(id);
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("delete")
    public JsonResult delete(@RequestBody UserDelParam userDelParam){
        userService.delete(userDelParam);
        return JsonResult.ok();
    }

}





























